package com.wodsy.phoenix.datadao.userc.dao.impl;

import com.wodsy.phoenix.commons.utils.DateUtils;
import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.userc.dao.MobileCaptchaDao;
import com.wodsy.phoenix.dataobject.userc.model.MobileCaptcha;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.sql.Date;


@Repository("mobileCaptchaEntityDao")
public class MobileCaptchaDaoImpl extends BaseHibernateDao<MobileCaptcha,Long> implements MobileCaptchaDao {

    public  static final int CAPTCHA_EXPIRE_TIME = 100000;//验证码过期时间
@Override
public Class<MobileCaptcha> getModelClass() {
    return MobileCaptcha.class;
    }

    @Override
    public MobileCaptcha getByMobile(Long mobile) {
        try {
            Criteria criteria = getSession().createCriteria(getModelClass());
            criteria.add(Restrictions.eq("mobile", mobile));
            criteria.add(Restrictions.ge("createTime", DateUtils.getDateBeforeSeconds(CAPTCHA_EXPIRE_TIME)));
            return (MobileCaptcha) criteria.uniqueResult();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Autowired
    @Qualifier(value="userc")
    public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
    }

    }
